package routes

import (
	"bluebell/controller"
	_ "bluebell/docs" // 千万不要忘了导入把你上一步生成的docs
	"bluebell/logger"
	"bluebell/middleware"
	"net/http"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	gs "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func Setup(model string) *gin.Engine {
	if model == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true), middleware.RateLimitMiddleware(2*time.Second, 1))
	r.Use(logger.GinLogger(), logger.GinRecovery(true))

	r.LoadHTMLFiles("./templates/index.html")
	r.StaticFS("/static", http.Dir("./static"))

	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

	// 根据时间或分数获取帖子列表
	r.GET("/api/v1/posts2", controller.GetPostListHandler2)

	v1 := r.Group("/api/v1")
	// 注册
	v1.POST("/signup", controller.SignUpHandler)
	// 登录
	v1.POST("/login", controller.LoginHandler)

	v1.Use(middleware.JWTAuthMiddleware())
	{
		v1.GET("/community", controller.CommunityHandler)
		v1.GET("/community/:id", controller.CommunityDetailHandler)
		v1.POST("/post", controller.PostHandler)
		v1.GET("/post/:id", controller.PostDetailHandler)
		v1.GET("/post/", controller.GetPostListHandler)
		v1.POST("/vote", controller.PostVoteController)

		v1.GET("post/all", controller.GetCommunityPostListHandler)
	}
	pprof.Register(r) // 注册pprof相关路由
	r.NoRoute(func(context *gin.Context) {
		context.JSON(http.StatusNotFound, gin.H{
			"msg": "404 not found",
		})
	})

	return r
}
